package com.warren.system.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.warren.response.Result;
import com.warren.response.ResultCode;
import com.warren.system.entity.Department;
import com.warren.system.entity.User;
import com.warren.system.mapper.DepartmentMapper;
import com.warren.system.mapper.UserMapper;
import com.warren.system.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author warren
 * @since 2021-03-22
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Department> findDeptAndCount() {

        return this.baseMapper.findDeptAndCount();
    }

    @Override
    public Department findDeptById(Long departmentId) {

        return  baseMapper.selectById(departmentId);
    }

    @Override
    public List<Department> getDeptAndCount(QueryWrapper<Department> queryWrapper) {
        return baseMapper.getDeptAndCount(queryWrapper);
    }

    @Override
    public Result addDept(Department department) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",department.getName());
        Integer integer = departmentMapper.selectCount(queryWrapper);
        if (integer != 0){
            return Result.error(ResultCode.DEPARTMENT_ALREADY_EXIST);
        }

        departmentMapper.insert(department);

        return Result.ok();
    }

    @Override
    public Result updateDept(Department department) {
        departmentMapper.updateById(department);
        return Result.ok();
    }

    @Override
    public Result deleteDept(Long id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id",id);
        Integer integer = userMapper.selectCount(queryWrapper);
        if (integer != 0){
            return Result.error(ResultCode.DEPARTMENT_HAVE_USER);
        }
        baseMapper.deleteById(id);
        return Result.ok();
    }

    @Override
    public void exportDeptExcel(HttpServletResponse response, List<Department> departmentLsit) throws IOException {
        String exportFileName = "DepartmentInfo.xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-disposition","attachment;filename="+exportFileName);
        EasyExcel.write(response.getOutputStream(),Department.class).
                sheet("部门信息")
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(departmentLsit);
    }
}
